package com.copera.cn.kaka;

import java.io.IOException;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.copera.cn.kaka.util.ActionUrls;
import com.copera.cn.kaka.util.ActivityManager;
import com.copera.cn.kaka.util.HttpUtils;
import com.copera.cn.kaka.util.JsonUtil;
import com.copera.cn.kaka.vo.PointPraiseSetVo;
import com.copera.cn.kaka.vo.ShopVo;

public class LoginActivity extends BaseActivity{
	
	private Button login_bt;
	private EditText et_name;
	private EditText et_pass;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityManager.getInstance().push(this);
		setContentView(R.layout.login);
		
		initView();
		initEvent();
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {

				case ActionUrls.FAILURE:
					Toast.makeText(LoginActivity.this, (String) msg.obj,
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		};
		//强制为竖屏 
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
	private void initView(){
		login_bt = (Button)findViewById(R.id.login_bt);
		et_name = (EditText)findViewById(R.id.et_name);
		et_pass = (EditText)findViewById(R.id.et_pass);
	}
	private void initEvent(){
 
		login_bt.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(isVilid()){
					final List<NameValuePair> params = getParams(et_name,et_pass);
					final ProgressDialog progressDialog = ProgressDialog.show(LoginActivity.this, "登录中..", "");
					if (params != null) {
						new Thread() {
							public void run() {
								try {
									//String json = HttpUtils.post(ActionUrls.URL_LOGIN, params,getAppCustomer().getHttpClient());
									String json = HttpUtils.get(ActionUrls.URL_LOGIN+"?account="+et_name.getText().toString()+"&password="+et_pass.getText().toString(), getAppCustomer().getHttpClient());
									try {
										Thread.sleep(3000);
									} catch (InterruptedException e) {
										e.printStackTrace();
									} 
									if (json != null) {
										JSONObject jo = new JSONObject(json);
										String message = jo.getString("message");
										if("登录成功".equals(message)){
											progressDialog.dismiss();
											ShopVo shop = (ShopVo) JsonUtil.toShop(json);
											save("shopId", shop.getId());
											save("shopName", shop.getShopName());
											save("address", shop.getAddress());
											save("trackToday", shop.getTrackToday());
											save("exchangeToday", shop.getExchangeToday());
											save("praiseCount", shop.getPraiseCount());
											save("shareCount", shop.getShareCount());
											save("headImage", shop.getHeadImage());
											getAppCustomer().setShop(shop);
											PointPraiseSetVo svo = JsonUtil.toPointPraiseSetVo(jo);
											getAppCustomer().setSetVo(svo);
											svo = JsonUtil.toShareSetVo(jo);
											getAppCustomer().setShareSetVo(svo);
											Intent intent = new Intent(LoginActivity.this,MainActivity.class);
											startActivity(intent);
											finish();
										}else{
											progressDialog.dismiss();
											Message msg = handler.obtainMessage(ActionUrls.FAILURE, message);
											handler.sendMessage(msg);
										}
									}else {
										progressDialog.dismiss();
										Message msg = handler.obtainMessage(ActionUrls.FAILURE, "网络异常，请重新登陆");
										handler.sendMessage(msg);
									}
								} catch (ClientProtocolException e) {
									e.printStackTrace();
								} catch (IOException e) {
									e.printStackTrace();
								} catch (JSONException e) {
									e.printStackTrace();
								}
								progressDialog.dismiss();
							};
						}.start();
					}
				}
			}
		});
				
	}
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		if (keyCode == KeyEvent.KEYCODE_BACK ){
			appExit();
		}
		return false;
	}
     
	private boolean isVilid(){
		if (et_name.getText().toString() == null || "".equals(et_name.getText().toString().trim())) {
			Toast.makeText(LoginActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (et_pass.getText().toString() == null || "".equals(et_pass.getText().toString().trim())) {
			Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}
}
